package com.example.exam.service.impl;

import com.example.exam.mapper.TExamPaperQuestionCustomerAnswerMapper;
import com.example.exam.pojo.DTO.ExamPaperAnswerDTO.ExamPaperAnswerUpdate;
import com.example.exam.pojo.DTO.ExamPaperAnswerDTO.ExamPaperSubmitDTO;
import com.example.exam.pojo.DTO.ExamPaperAnswerDTO.ExamPaperSubmitItemDTO;
import com.example.exam.pojo.Enum.QuestionTypeEnum;
import com.example.exam.pojo.entity.TExamPaperQuestionCustomerAnswer;
import com.example.exam.pojo.entity.TTextContent;
import com.example.exam.service.ICustomerAnswerService;
import com.example.exam.service.ITextContentService;
import com.example.exam.tools.ExamUtil;
import com.example.exam.tools.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CustomerAnswerServiceImpl implements ICustomerAnswerService {

    @Autowired
    private ITextContentService textContentService;

    @Autowired
    private TExamPaperQuestionCustomerAnswerMapper customerAnswerMapper;

    @Override
    public int updateScore(List<ExamPaperAnswerUpdate> examPaperAnswerUpdates) {
        return customerAnswerMapper.updateScore(examPaperAnswerUpdates);
    }

    @Override
    public TExamPaperQuestionCustomerAnswer selectById(Integer id) {
        log.debug("开始处理【根据ID查询用户答案】的业务");
        TExamPaperQuestionCustomerAnswer selectById = customerAnswerMapper.selectById(id);
        return selectById;
    }

    @Override
    public List<TExamPaperQuestionCustomerAnswer> listByPaperAnswerId(Integer id) {
        return customerAnswerMapper.ListByPaperAnswerId(id);
    }

    /**
     * 将用户的答案和题目封装到itemDTO中
     * @param answer
     * @return
     */
    @Override
    public ExamPaperSubmitItemDTO getCustomerAnswer(TExamPaperQuestionCustomerAnswer answer) {
        log.debug("开始处理【将用户的答案和题目封装到itemDTO】的业务。。。answer={}",answer);
        ExamPaperSubmitItemDTO itemDTO = new ExamPaperSubmitItemDTO();
        itemDTO.setId(answer.getId());
        itemDTO.setQuestionId(answer.getQuestionId());
        itemDTO.setDoRight(answer.getDoRight());
        itemDTO.setItemOrder(answer.getItemOrder());
        itemDTO.setQuestionScore(answer.getQuestionScore());
        itemDTO.setScore(answer.getCustomerScore());
        setSpecialAnswer(itemDTO,answer);
        return itemDTO;
    }

    /**
     * 根据不同题型显示正确答案
     * @param itemDTO
     * @param answer
     */
    private void setSpecialAnswer(ExamPaperSubmitItemDTO itemDTO,
                                  TExamPaperQuestionCustomerAnswer answer){
        log.debug("开始处理【根据不同题型显示正确答案】的业务");
        QuestionTypeEnum type = QuestionTypeEnum.fromCode(answer.getQuestionType());
        log.debug("正在获取问题的类型，type={}",type);
        switch (type){
            //多选题选项需要用数组接收，其他题目不用
            case MultipleChoice:
                log.debug("正在处理多选题的答题信息");
                itemDTO.setContent(answer.getAnswer());
                itemDTO.setContentArray(ExamUtil.contentToArray(answer.getAnswer()));
                break;
            default:
                log.debug("正在处理其他问题的答题信息");
                if(QuestionTypeEnum.needSaveTextContent(answer.getQuestionType())){
                   TTextContent content1 =  textContentService.selectById(answer.getTextContentId());
                    itemDTO.setContent(content1.getContent());
                }else{
                    itemDTO.setContent(answer.getAnswer());
                }
                break;
        }
    }
}
